import { formFieldRule } from "@/components/FormCreate/interface/fieldRule";
import { DicOption } from "@/api/interface/Common/DicOption";
import { TreeSelect } from "@/api/interface/Common/TreeSelect"; //传入列实现修改属性的通用方法

//编辑页的字段表单配置
export class EditColumns extends Array<formFieldRule> {
  constructor(...args: any[]) {
    super(...args);
  }

  //根据field查找一个字段的rule
  findItemRule(fieldName: string): formFieldRule {
    for (let i = 0; i < this.length; i++) {
      let column = this[i];
      if (column.field == fieldName) {
        return column;
      }
    }
    return {} as formFieldRule;
  }

  //根据field设置一个字段的rule，会覆盖原有的
  setItemRule(fieldName: string, item: formFieldRule): boolean {
    for (let i = 0; i < this.length; i++) {
      let column = this[i];
      if (column.field == fieldName) {
        this[i] = item;
        return true;
      }
    }
    return false;
  }

  //属性化值，更新操作时用到
  initValues(data: any) {
    // for循环
    for (let i = 0; i < this.length; i++) {
      let column = this[i];
      if (column.field && data[column.field]) {
        column.value = data[column.field];
      } else {
        column.value = "";
      }
    }
    return this;
  }

  //设置表单隐藏
  setHidden(fieldName: string, isHidden: boolean) {
    this.resetFormRule(fieldName, { hidden: isHidden });
  }

  //设置下拉框可以本地搜索
  setSelectFilter(fieldName: string, isFilterable: boolean) {
    this.resetFormRule(fieldName, {
      props: {
        filterable: isFilterable
      }
    });
  }

  setTitle(fieldName: string, _title: string) {
    this.resetFormRule(fieldName, { title: _title });
  }

  //设置表单的值
  setValue(fieldName: string, value_: any) {
    this.resetFormRule(fieldName, { value: value_ });
  }

  //设置表单非必填
  setNotRequired(fieldName: string) {
    this.resetFormRule(fieldName, {
      validate: [{ required: false }]
    });
  }

  setInfo(fieldName: string, content: any) {
    this.resetFormRule(fieldName, { info: content });
  }

  setDisplay(fieldName: string, isDisplay: boolean) {
    this.resetFormRule(fieldName, { display: isDisplay });
  }

  //设置radio,select,checkbox等组件option选择项
  setOptions(fieldName: string, OptionsData: Array<DicOption>) {
    this.resetFormRule(fieldName, { options: OptionsData });
  }

  //设置组件占一行的多少默认12，可选0-24
  setRowCols(fieldName: string, cols: Number) {
    this.resetFormRule(fieldName, {
      col: {
        span: cols
      }
    });
  }

  //设置css class名称
  setClass(fieldName: string, classStr: string) {
    this.resetFormRule(fieldName, { class: classStr });
  }

  //重新设置表单的字段名称
  setFieldName(fieldName: string, newFieldName: string) {
    this.resetFormRule(fieldName, { field: newFieldName });
  }

  //设置css style名称
  setStyle(fieldName: string, StyleStr: string) {
    this.resetFormRule(fieldName, { class: StyleStr });
  }

  //设置input表单提示词
  setInputPlaceholder(fieldName: string, placeholderContent: string) {
    this.resetFormRuleProps(fieldName, { placeholder: placeholderContent });
  }

  //下拉框是否为多选
  setSelectIsMultiple(fieldName: string, isMultiple: string) {
    this.resetFormRuleProps(fieldName, { multiple: isMultiple });
  }

  //设置input的最大长度
  setInputMaxLength(fieldName: string, maxLength: number) {
    this.resetFormRuleProps(fieldName, { maxlength: maxLength });
  }

  //设置input的最小长度
  setInputMinLength(fieldName: string, minLength: number) {
    this.resetFormRuleProps(fieldName, { minlength: minLength });
  }

  //设置表单禁用
  setDisabled(fieldName: string, isDisabled: boolean) {
    this.resetFormRuleProps(fieldName, { disabled: isDisabled });
  }

  //设置input表单类型为textarea,同时设置行数,isAutosize表示是否自适应高度
  setInputToTextarea(fieldName: string, rows: number = 3, isAutosize: boolean = false) {
    this.resetFormRuleProps(fieldName, {
      type: "textarea",
      rows: rows,
      autosize: isAutosize
    });
  }

  //根据field设置一个字段的rule，不会覆盖原有的
  resetFormRule(fieldName: string, overWriteRule: formFieldRule) {
    let column = this.findItemRule(fieldName);
    Object.assign(column, overWriteRule);
  }

  //重置一个表单的rule.props
  resetFormRuleProps(fieldName: string, overWriteProps: any) {
    let column = this.findItemRule(fieldName);
    Object.assign(column.props ?? {}, overWriteProps);
  }

  //重置一个表单的rule.events
  resetFormRuleOn(fieldName: string, overWriteProps: any) {
    let column = this.findItemRule(fieldName);
    Object.assign(column.on ?? {}, overWriteProps);
  }
}
